import CryptoJS from "crypto-js";
import JSEncrypt from "jsencrypt";

export const createRandomStr = (lenght)=>{
  let str = '';
  while (lenght-- > 0) {
      let num = Math.floor(Math.random() * 26);
      str += String.fromCharCode(num + 97);
  }
  return str;
};
// AES key对数据进行加密
export const aesEncryteData = (str,key,iv) => {
  let asekey = CryptoJS.enc.Utf8.parse(key);
  //加密向量16位
  let aesiv = CryptoJS.enc.Utf8.parse(iv);
  
  let encrypted = CryptoJS.AES.encrypt(str, asekey, {
    iv: aesiv,
    mode: CryptoJS.mode.CBC,
    padding: CryptoJS.pad.ZeroPadding
  });
  return encrypted;
};


const encryptor = new JSEncrypt();
// 服务端的公钥对AES key加密
export const rsaEncrypt = (aesKey) => {
  let publicKey = uni.getStorageSync("key");

  encryptor.setPublicKey(publicKey);
  // encryptor.setPadding(JSEncrypt.RSA_PKCS1_PADDING);

  return encryptor.encrypt(aesKey);
};